package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    //获取当前登录用户的用户名
    @RequestMapping("/getUsername")
    public Result getUsername()throws Exception{
        try{
            User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
        }catch (Exception e){
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }
    //分页查询
    @RequestMapping("/pageQuery")
    public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = userService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody User user, Integer[] userIds){
        try {
            user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
            userService.add(user, userIds);

        }catch (Exception e){
            //新增失败
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
        //新增成功
        return new Result(true, MessageConstant.ADD_USER_SUCCESS);
    }


    //根据id查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        User user = userService.findById(id);
        if(user != null){
            Result result = new Result(true, MessageConstant.QUERY_USER_SUCCESS);
            result.setData(user);
            return result;
        }
        return new Result(false,MessageConstant.QUERY_USER_FAIL);
    }

    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer id){
        try{
            List<Integer> roleIds = userService.findRoleIdsByUserId(id);
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, roleIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user, Integer[] roleIds){
        try {
            userService.edit(user,roleIds);
        }catch (Exception e){
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
    }

    //查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        List<User> userList = userService.findAll();
        if(userList != null && userList.size() > 0){
            Result result = new Result(true, MessageConstant.QUERY_USER_SUCCESS);
            result.setData(userList);
            return result;
        }
        return new Result(false, MessageConstant.QUERY_USER_FAIL);
    }


    //根据检查组ID查询检查组包含的多个检查项ID
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            userService.delete(id);
            return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
    }
	
	//动态展示菜单
	@RequestMapping("/findSidebar")
    public Result findSidebar(){
        try {
            org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            LinkedHashSet<Map<String, Object>> set = userService.findMenusByUsername(user.getUsername());
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, set);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }
}
